package com.aps.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.aps.entity.ApsExeption;
import com.aps.entity.domain.device.DeviceCenter;
import com.aps.entity.domain.order.OrderDish;
import com.aps.entity.dto.KdsDeviceOpeDTO;
import com.aps.entity.eunm.AdjustType;
import com.aps.entity.eunm.DishStatus;
import com.aps.entity.eunm.ResultType;
import com.aps.page.BaseQuery;
import com.aps.page.Pagination;
import com.aps.service.IDeviceCenterService;
import com.aps.service.rep.*;
import com.aps.util.JdbcQueryUtil;
import com.aps.util.ScheduleComponent;

/**
 * @author liaoguangan
 * @description <设备服务实现类>
 * @date 2018/4/1 15:56
 */
@Service
public class DeviceCenterServiceImpl implements IDeviceCenterService {

    @Autowired
    private DeviceCenterRep deviceCenterRep;

    @Autowired
    private OrderDishRep orderDishRep;

    @Autowired
    private ScheduleComponent scheduleComponent;

    @Autowired
    private DeviceModuleRep deviceModuleRep;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private DeviceTypeRep deviceTypeRep;

    @Autowired
    private StoreReq storeReq;

    /**
     * 添加设备
     *
     * @param deviceCenter
     * @return
     */
    @Override
    public DeviceCenter add(DeviceCenter deviceCenter) {
        if (null != deviceCenter.getId()) { //修改
            //校验CODE
            DeviceCenter exist = deviceCenterRep.findByCode(deviceCenter.getCode());
            if (null != exist && !exist.getId().equals(deviceCenter.getCode())) {
                throw new ApsExeption(ResultType.DEVICE_CODE_EXIST);
            }
        } else {
            if (this.checkDeviceCode(deviceCenter.getCode())) {
                throw new ApsExeption(ResultType.DEVICE_CODE_EXIST);
            }
        }
        return deviceCenterRep.save(deviceCenter);
    }

    /**
     * 查询设备
     *
     * @param id
     * @return
     */
    @Override
    public DeviceCenter get(String id) {
        String sql = "select adc.id, adc.code, adc.name, adc.device_type_code, "
                + " adt.name as device_type_name, adc.store_code, aps.name as store_name"
                + " from aps_device_center adc "
                + " left join aps_device_type adt on adc.device_type_code = adt.code "
                + " left join aps_store aps on adc.store_code = aps.code where adc.id = ?";

        DeviceCenter dc = new JdbcQueryUtil<>(DeviceCenter.class).query(jdbcTemplate, sql,
                new Object[] { id });
        if (null == dc) {
            throw new ApsExeption(ResultType.DEVICE_NOT_EXIST);
        }

        return dc;
    }

    /**
     * 删除设备
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        DeviceCenter dc = deviceCenterRep.findOne(id);
        if (null == dc) {
            throw new ApsExeption(ResultType.DEVICE_NOT_EXIST);
        }

        dc.setIsDeleted(true);
        deviceCenterRep.save(dc);
    }

    /**
     * 检查设备代码是否存在
     *
     * @param code
     * @return
     */
    @Override
    public Boolean checkDeviceCode(String code) {
        DeviceCenter dc = deviceCenterRep.findByCode(code);
        return dc != null;
    }

    /**
     * KDS反馈设备是否可用
     *
     * @param kdsDeviceOpeDTO
     */
    @Override
    public void fix(KdsDeviceOpeDTO kdsDeviceOpeDTO) {
        DeviceCenter deviceCenter = deviceCenterRep.findByCode(kdsDeviceOpeDTO.getCtrSlotid());
        switch (kdsDeviceOpeDTO.getAdjustType()) {
            case USED: { //人为占用
                deviceCenter.setStatus(AdjustType.USED);
                break;
            }
            case STOP: { //停用设备
                deviceCenter.setStatus(AdjustType.STOP);
                break;
            }
            case RELEASE: { //释放修正
                OrderDish orderDish = orderDishRep.findByOrderNumAndDishSeq(
                        kdsDeviceOpeDTO.getOrderNum(), kdsDeviceOpeDTO.getDishSeq());
                orderDish.setStatus(DishStatus.UNSTART);
                orderDish.setStartTime(null);
                orderDishRep.save(orderDish);

                //释放设备
                deviceCenter.setStatus(AdjustType.RELEASE);
                break;
            }
            default: {

            }
        }
        deviceCenterRep.save(deviceCenter);

        //触发排程
        scheduleComponent.setScheduleSoon(true);
    }

    /**
     * KDS查询设备信息
     *
     * @return
     */
    @Override
    public List<DeviceCenter> findAll() {
        return deviceCenterRep.findAll();
    }

    /**
     * 分页查询设备
     *
     * @param baseQuery
     * @return
     */
    @Override
    public Pagination<DeviceCenter> listDevice(BaseQuery baseQuery) {
        String sql = "select adc.id, adc.code, adc.name, adt.name as device_type_name, "
                + " aps.name as store_name, adc.enable_start_time, adc.enable_end_time "
                + " from aps_device_center adc "
                + " left join aps_device_type adt on adc.device_type_code = adt.code "
                + " left join aps_store aps on adc.store_code = aps.code ";

        return new Pagination<DeviceCenter>().search(jdbcTemplate, DeviceCenter.class, sql,
                new Object[] {}, baseQuery, "adc");
    }
}
